package com.etoak.controller;

import com.etoak.common.utils.uuid.IdUtils;
import com.etoak.entity.Product;
import com.etoak.entity.ProductLine;
import com.etoak.service.IProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Checklist库产品controller
 *
 * @author zhixuebin
 * @date 2022-05-12
 */
@RestController
@RequestMapping("/goertek/checklist/product")
public class ProductController extends BaseController {

    @Autowired
    IProductService productService;

    /**
     * 添加产品
     * @param product
     * @return
     */
    @Transactional
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Product product){
        //验重
        List<Product> search = productService.getProduct(product);

        if(StringUtils.isNull(search) || search.size() < 1){
            //设置主键
            String id = IdUtils.simpleUUID();
            product.setId(id);
            productService.add(product);
        }else{
            return AjaxResult.error("产品已存在！");
        }
        return AjaxResult.success("产品添加成功");
    }

    /**
     * 根据产品线id查询产品
     * @param productLineId
     * @return
     */
    @GetMapping("/getProductByProductLine")
    public AjaxResult getProductByProductLine(@RequestParam("productLineId") String productLineId){
        List<Product> productList = productService.getProductByProductLine(productLineId);
        return AjaxResult.success(productList);
    }

}
